package com.easy.controller;

import com.easy.bean.Admin;
import com.easy.bean.User;
import com.easy.service.LoginService;
import com.easy.util.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/login")
@CrossOrigin
public class LoginController {
    @Autowired
    LoginService loginService;

    @RequestMapping("/captcha")
    public Result Captcha() {
        return Result.success(CaptchaUtil.getCaptcha());
    }

    @PostMapping("/dologin")
    public Result login(Admin admin, @RequestParam("captcha") String captcha, @RequestParam("key") String key) {
        //1.验证验证码
        if (!CaptchaUtil.checkCaptcha(key, captcha)) {
            return Result.fail(500, "验证码错误");
        }
        //登录
        try {
            String token = loginService.login(admin);
            //删除验证码
            return Result.success(token);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, e.getMessage());
        }
    }
@PostMapping("/dologinuser")
    public Result loginUser(User user, @RequestParam("captcha") String captcha, @RequestParam("key") String key) {
        //1.验证验证码
        if (!CaptchaUtil.checkCaptcha(key, captcha)) {
            return Result.fail(500, "验证码错误");
        }
        //登录
        try {
            String token = loginService.loginUser(user);
            //删除验证码
            return Result.success(token);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, e.getMessage());
        }
    }
    @GetMapping("/logininfo")
    public Result logininfo(HttpServletRequest request) {
        String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
        Admin admin = loginService.logininfo(token);
        return Result.success(admin);
    }
    @GetMapping("/userinfo")
    public Result userinfo(HttpServletRequest request) {
        String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
        User user = loginService.userinfo(token);
        return Result.success(user);
    }



    @GetMapping("/sendemailcode")
    public Result sendEmailCode(@RequestParam("email") String email) {
        try {
            Admin admin = loginService.getAdminByEmail(email);
            if (admin == null) {
                return Result.fail(500, "邮箱不存在");
            }
            String code = EmailCodeUtil.sendCode(email);
            return Result.success(code);
        }
        catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, e.getMessage());
        }
    }
    @GetMapping("/senduseremailcode")
    public Result sendUserEmailCode(@RequestParam(value="email",required = true) String email) {
        try {
            User user = loginService.getUserByEmail(email);
            if (user == null) {
                return Result.fail(500, "邮箱不存在");
            }
            String code = EmailCodeUtil.sendCode(email);
            return Result.success(code);
        }
        catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, e.getMessage());
        }
    }
    @PostMapping("/dologinbyemail")
    public Result loginByEmail(@RequestParam("email") String  email, @RequestParam("code") String code) {
        try {
            // 验证邮箱验证码
            if (!EmailCodeUtil.verifyCode(email, code)) {
                return Result.fail(500, "验证码错误或已过期");
            }

            // 获取管理员信息
            Admin admin = loginService.getAdminByEmail(email);
            if (admin == null) {
                return Result.fail(500, "该邮箱未关联任何管理员账户");
            }

            // 生成token并登录
            String token = loginService.loginByEmailCode(admin);
            return Result.success(token);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "登录失败：" + e.getMessage());
        }
    }
    @PostMapping("/dologinuserbyemail")
    public Result loginUserByEmail(@RequestParam("email") String  email, @RequestParam("code") String code) {
        try {
            // 验证邮箱验证码
            if (!EmailCodeUtil.verifyCode(email, code)) {
                return Result.fail(500, "验证码错误或已过期");
            }

            // 获取用户信息
            User user = loginService.getUserByEmail(email);
            if (user == null) {
                return Result.fail(500, "该邮箱未关联任何用户账户");
            }

            // 生成token并登录
            String token = loginService.loginUserByEmailCode(user);
            return Result.success(token);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "登录失败：" + e.getMessage());
        }}
    }
